<?php

/*
=====================================================
 FluxBB - Delux Board Converstations (DBC)
-----------------------------------------------------
 http://dbc-forum.com/
 http://fluxbb.org/
-----------------------------------------------------
 Copyright (c) 2010,2011 FluxBB - Delux Board Conversations (DBC)
-----------------------------------------------------
 Предсловие: Данный скрипт (DBC) не является работой с нуля, то 
 есть ядром скрипта всё также остаётся FuxBB,но теперь в оболочке
 DBC.
=====================================================
*/

// Tell engine.php to use the admin template
define('DBC_ADMIN_CONSOLE', 1);

define('DBC_ROOT', './');
require DBC_ROOT.'engine/common.php';
require DBC_ROOT.'engine/common_admin.php';


if (!$DBC_user['is_admmod'] || ($DBC_user['g_moderator'] == '1' && $DBC_config['o_censoring'] == '0'))
	message($lang_common['No permission']);

// Load the admin_censoring.php language file
require DBC_ROOT.'language/'.$admin_language.'/admin_censoring.php';

// Add a censor word
if (isset($_POST['add_word']))
{
	confirm_referrer('admin_censoring.php');

	$search_for = DBC_trim($_POST['new_search_for']);
	$replace_with = DBC_trim($_POST['new_replace_with']);

	if ($search_for == '' || $replace_with == '')
		message($lang_admin_censoring['Must enter both message']);

	$db->query('INSERT INTO '.$db->prefix.'censoring (search_for, replace_with) VALUES (\''.$db->escape($search_for).'\', \''.$db->escape($replace_with).'\')') or error('Unable to add censor word', __FILE__, __LINE__, $db->error());

	$slovo = $db->escape($search_for);
	$result = $db->query('SELECT COUNT(id) FROM '.$db->prefix.'censoring WHERE search_for IN (\''.$slovo.'\',\'*'.$slovo.'\',\''.$slovo.'*\',\'*'.$slovo.'*\')') or error('Unable to fetch censor word', __FILE__, __LINE__, $db->error());
	$num_slov = $db->result($result);
	
	// Regenerate the bans cache
	if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
		require DBC_ROOT.'engine/cache.php';

	generate_censor_cache();

	redirect('admin_censoring.php'.(($num_slov > 1) ? '?censorflag=1' : ''), $lang_admin_censoring['Word added redirect']);
}

// Update a censor word
else if (isset($_POST['update']))
{
	confirm_referrer('admin_censoring.php');

	$id = intval(key($_POST['update']));

	$search_for = DBC_trim($_POST['search_for'][$id]);
	$replace_with = DBC_trim($_POST['replace_with'][$id]);

	if ($search_for == '' || $replace_with == '')
		message($lang_admin_censoring['Must search both message']);

	$db->query('UPDATE '.$db->prefix.'censoring SET search_for=\''.$db->escape($search_for).'\', replace_with=\''.$db->escape($replace_with).'\' WHERE id='.$id) or error('Unable to update censor word', __FILE__, __LINE__, $db->error());

	$slovo = $db->escape($search_for);
	$result = $db->query('SELECT COUNT(id) FROM '.$db->prefix.'censoring WHERE search_for IN (\''.$slovo.'\',\'*'.$slovo.'\',\''.$slovo.'*\',\'*'.$slovo.'*\')') or error('Unable to fetch censor word', __FILE__, __LINE__, $db->error());
	$num_slov = $db->result($result);

	// Regenerate the bans cache
	if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
		require DBC_ROOT.'engine/cache.php';

	generate_censor_cache();

	redirect('admin_censoring.php'.(($num_slov > 1) ? '?censorflag=1' : ''), $lang_admin_censoring['Word updated redirect']);
}

// Remove a censor word
else if (isset($_POST['remove']))
{
	confirm_referrer('admin_censoring.php');

	$id = intval(key($_POST['remove']));

	$db->query('DELETE FROM '.$db->prefix.'censoring WHERE id='.$id) or error('Unable to delete censor word', __FILE__, __LINE__, $db->error());

	// Regenerate the bans cache
	if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
		require DBC_ROOT.'engine/cache.php';

	generate_censor_cache();

	redirect('admin_censoring.php',  $lang_admin_censoring['Word removed redirect']);
}

$page_title = array(DBC_htmlspecialchars($DBC_config['o_board_title']), $lang_admin_common['Admin'], $lang_admin_common['Censoring']);
$focus_element = array('censoring', 'new_search_for');
define('DBC_ACTIVE_PAGE', 'admin');
require DBC_ROOT.'engine/engine.php';

generate_admin_menu('censoring');

?>
	<div class="blockform">

<table width="100%" height="38" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" width="28" class="bg_forum_title_left"><img src="engine/skins/images/spacer.gif" border="0"></td>
<td align="left" class="bg_forum_title_center"><span class="cat_title"><?php echo $lang_admin_censoring['Censoring head'] ?></span></td>
<td align="center" width="28" class="bg_forum_title_right"><img src="engine/skins/images/spacer.gif" border="0"></td>
</tr></table>

<table class="forum_adv_table"  width="100%" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" class="forum_adv_tleft" width="2"><img src="engine/skins/images/spacer.gif" border="0"></td>
<td align="left" width="100%"><div class="forum_adv_c">

			<form id="censoring" method="post" action="admin_censoring.php?action=foo">
				<input type="hidden" name="csrf_hash" value="<?php echo csrf_hash() ?>" />
				<div class="inform">
					<fieldset>
						<div class="login_info"><?php echo $lang_admin_censoring['Add word subhead'] ?></div>
						<div class="infldset">
							<p><?php echo $lang_admin_censoring['Add word info'].($DBC_user['g_id'] != DBC_ADMIN ? '' : ' '.($DBC_config['o_censoring'] == '1' ? sprintf($lang_admin_censoring['Censoring enabled'], '<a href="admin_options.php#censoring">'.$lang_admin_common['Options'].'</a>') : sprintf($lang_admin_censoring['Censoring disabled'], '<a href="admin_options.php#censoring">'.$lang_admin_common['Options'].'</a>'))) ?></p>
							<table cellspacing="0">
							<thead>
								<tr>
									<th class="tcl" scope="col"><?php echo $lang_admin_censoring['Censored word label'] ?></th>
									<th class="tc2" scope="col"><?php echo $lang_admin_censoring['Replacement label'] ?></th>
									<th class="hidehead" scope="col"><?php echo $lang_admin_censoring['Action label'] ?></th>
								</tr>
							</thead>
							<tbody>
								<tr>
									<td class="tcl"><input type="text" class="input_text" name="new_search_for" size="24" maxlength="60" tabindex="1" /></td>
									<td class="tc2"><input type="text" class="input_text" name="new_replace_with" size="24" maxlength="60" tabindex="2" /></td>
									<td><input type="submit" name="add_word" value="<?php echo $lang_admin_common['Add'] ?>" tabindex="3" /></td>
								</tr>
							</tbody>
							</table>
						</div>
					</fieldset>
				</div>
<?php if (isset($_GET['censorflag'])): ?>
				<div class="inform">
					<fieldset>
						<div class="login_info"><?php echo $lang_admin_censoring['Double'] ?></div>
						<div class="infldset">
							<p><span style="color: red"><b><?php echo $lang_admin_censoring['Double2'] ?></b></span></p>
						</div>
					</fieldset>
				</div>
<?php endif; ?>
				<div class="inform">
					<fieldset>
						<div class="login_info"><?php echo $lang_admin_censoring['Edit remove subhead'] ?></div>
						<div class="infldset">
<?php

$result = $db->query('SELECT id, search_for, replace_with FROM '.$db->prefix.'censoring ORDER BY id') or error('Unable to fetch censor word list', __FILE__, __LINE__, $db->error());
if ($db->num_rows($result))
{

?>
							<table cellspacing="0" >
							<thead>
								<tr>
									<th class="tcl" scope="col"><?php echo $lang_admin_censoring['Censored word label'] ?></th>
									<th class="tc2" scope="col"><?php echo $lang_admin_censoring['Replacement label'] ?></th>
									<th class="hidehead" scope="col"><?php echo $lang_admin_censoring['Action label'] ?></th>
								</tr>
							</thead>
							<tbody>
<?php

	while ($cur_word = $db->fetch_assoc($result))
		echo "\t\t\t\t\t\t\t\t".'<tr><td class="tcl"><input type="text" class="input_text" name="search_for['.$cur_word['id'].']" value="'.DBC_htmlspecialchars($cur_word['search_for']).'" size="24" maxlength="60" /></td><td class="tc2"><input type="text" class="input_text" name="replace_with['.$cur_word['id'].']" value="'.DBC_htmlspecialchars($cur_word['replace_with']).'" size="24" maxlength="60" /></td><td><input type="submit" name="update['.$cur_word['id'].']" value="'.$lang_admin_common['Update'].'" />&#160;<input type="submit" name="remove['.$cur_word['id'].']" value="'.$lang_admin_common['Remove'].'" /></td></tr>'."\n";

?>
							</tbody>
							</table>
<?php

}
else
	echo "\t\t\t\t\t\t\t".'<p>'.$lang_admin_censoring['No words in list'].'</p>'."\n";

?>
						</div>
					</fieldset>
				</div>
			</form>
<br/>
</div></td>
<td align="left" class="forum_adv_tright" width="2"><img src="engine/skins/images/spacer.gif" border="0"></td>
</td></tr></table>
<table width="100%" height="23" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" width="30" class="bg_forum_bott_left"><img src="engine/skins/images/spacer.gif" border="0"></td>
<td align="left" class="bg_forum_bott_center"><img src="engine/skins/images/spacer.gif" border="0"></td>
<td align="center" width="30" class="bg_forum_bott_right"><img src="engine/skins/images/spacer.gif" border="0"></td>
</tr></thead></table>
	</div>
	<div class="clearer"></div>
</div>
<?php

require DBC_ROOT.'engine/footer.php';
